package com.siss.tenancy.util;

import java.io.UnsupportedEncodingException;

/**
 * 文件名：StringCharsetUtil.java
 * 字符编码工具类
 * 版权：srba
 * 修改人：王行江
 * 修改内容：新增
 * 修改时间：2016-08-19
 **/
public class StringCharsetUtil {
    /** 7位ASCII字符，也叫作ISO646-US、Unicode字符集的基本拉丁块      */
    public static final String US_ASCII = "US-ASCII";
    /** ISO拉丁字母表 No.1，也叫做ISO-LATIN-1     */
    public static final String ISO_8859_1 = "ISO-8859-1";
    /** 8 位 UCS 转换格式     */
    public static final String UTF_8 = "UTF-8";
    /** 16 位 UCS 转换格式，Big Endian(最低地址存放高位字节）字节顺序     */
    public static final String UTF_16BE = "UTF-16BE";
    /** 16 位 UCS 转换格式，Litter Endian（最高地址存放地位字节）字节顺序     */
    public static final String UTF_16LE = "UTF-16LE";
    /** 16 位 UCS 转换格式，字节顺序由可选的字节顺序标记来标识     */
    public static final String UTF_16 = "UTF-16";
    /** 中文超大字符集     **/
    public static final String GBK = "GBK";
    
    public static final String GB2312 = "GB2312";

    public static final String[] charsets = {US_ASCII,ISO_8859_1,UTF_8,UTF_16BE,UTF_16LE,UTF_16,GBK,GB2312};

    /** 将字符编码转换成US-ASCII码     */
    @SuppressWarnings("static-access")
	public String toASCII(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, US_ASCII);
    }
    
    /** 将字符编码转换成ISO-8859-1     */
    @SuppressWarnings("static-access")
	public String toISO_8859_1(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, ISO_8859_1);
    }
    
    /** 将字符编码转换成UTF-8     */
    @SuppressWarnings("static-access")
	public String toUTF_8(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, UTF_8);
    }
    
    /** 将字符编码转换成UTF-16BE     */
    @SuppressWarnings("static-access")
	public String toUTF_16BE(String str) throws UnsupportedEncodingException{
        return this.changeCharset(str, UTF_16BE);
    }
    
    /** 将字符编码转换成UTF-16LE     */
    @SuppressWarnings("static-access")
	public String toUTF_16LE(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, UTF_16LE);
    }
    
    /** 将字符编码转换成UTF-16     */
    @SuppressWarnings("static-access")
	public String toUTF_16(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, UTF_16);
    }
    
    /** 将字符编码转换成GBK     */
    @SuppressWarnings("static-access")
	public String toGBK(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, GBK);
    }
    
    /** 将字符编码转换成GB2312     */
    @SuppressWarnings("static-access")
	public String toGB2312(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str,GB2312);
    }
    
    /**
     * 字符串编码转换的实现方法
     * @param str    待转换的字符串
     * @param newCharset    目标编码
     */
    public static String changeCharset(String str, String newCharset) throws UnsupportedEncodingException {
        if(str != null) {
            //用默认字符编码解码字符串。与系统相关，中文windows默认为GB2312
            byte[] bs = str.getBytes();
            return new String(bs, newCharset);    //用新的字符编码生成字符串
        }
        return null;
    }
    
    /**
     * 字符串编码转换的实现方法
     * @param str    待转换的字符串
     * @param oldCharset    源字符集
     * @param newCharset    目标字符集
     */
    public static String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException {
        if(str != null) {
            //用源字符编码解码字符串
            byte[] bs = str.getBytes(oldCharset);
            return new String(bs, newCharset);
        }
        return null;
    }
    /**
     * iso8859-1 转utf-8
     * @param destination
     * @return
     * @throws UnsupportedEncodingException
     */

    public static String iso8859_1ToUTF_8(String destination){
        try {
            if (destination != null) {
                if (destination.equals(new String(destination.getBytes("iso8859-1"), "iso8859-1"))) {
                    destination = new String(destination.getBytes("iso8859-1"), "utf-8");
                }
            }
            return destination;
        }catch (Exception e){
            e.printStackTrace();
            return destination;
        }
    }

    public static String charset2Utf8(String destination){
        for (int i = 0; i < charsets.length; i++){
            try {
                if (destination != null) {
                    if (destination.equals(new String(destination.getBytes(charsets[i]), charsets[i]))) {
                        destination = new String(destination.getBytes(charsets[i]), "utf-8");
                        return destination;
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return destination;
    }


    /**
     * 循环输出相关转码，用与确认转码规则
     * @param str
     * @throws UnsupportedEncodingException
     */
    public static void changeForCharset(String str) throws UnsupportedEncodingException{
    	System.err.println(changeCharset(str, "GBK", "UTF-8") );
    	System.err.println(changeCharset(str, "GB2312", "UTF-8"));
    	System.err.println(changeCharset(str, "ISO-8859-1", "UTF-8"));
    	System.err.println(changeCharset(str, "UTF-8", "GB2312"));
    	System.err.println(changeCharset(str, "ISO_8859_1", "GB2312"));
    	System.err.println(changeCharset(str, "GBK", "GB2312"));
    	System.err.println(changeCharset(str, "GB2312", "ISO-8859-1"));
    	System.err.println(changeCharset(str, "GBK", "ISO-8859-1"));
    	System.err.println(changeCharset(str, "UTF-8", "ISO-8859-1"));
    	System.err.println(changeCharset(str, "GB2312", "GBK"));
    	System.err.println(changeCharset(str, "ISO-8859-1", "GBK"));
    	System.err.println(changeCharset(str, "UTF-8", "GBK"));
    }
    
    public static void main(String[] args) throws UnsupportedEncodingException {
    	changeForCharset("é¾ç¾ç³");
    	System.out.println(iso8859_1ToUTF_8("é¾ç¾ç³"));
    	System.out.println(iso8859_1ToUTF_8("龙美琳"));
	}
}